<?php

use Drupal\shusy_errand\Entity\ShusyErrand;
use Drupal\shusy_errand\Entity\ShusyErrandOrder;

function shusy_errand_shusy_errand_add_page() {
  $e = new ShusyErrand();
  module_load_include('inc', 'shusy_errand', 'shusy_errand.forms');
  return drupal_get_form('shusy_errand_shusy_errand_entity_form', $e, 'add');
}

function shusy_errand_shusy_errand_view_page(ShusyErrand $e) {
  return entity_view($e->entityType(), [$e]);
}

function shusy_errand_shusy_errand_edit_page(ShusyErrand $e) {
  module_load_include('inc', 'shusy_errand', 'shusy_errand.forms');
  return drupal_get_form('shusy_errand_shusy_errand_entity_form', $e, 'edit');
}

function shusy_errand_shusy_errand_delete_page(ShusyErrand $e) {
  module_load_include('inc', 'shusy_errand', 'shusy_errand.forms');
  return drupal_get_form('shusy_errand_shusy_errand_delete_form', $e);
}

function shusy_errand_shusy_errand_order_add_page() {
  $e = new ShusyErrandOrder();
  module_load_include('inc', 'shusy_errand', 'shusy_errand.forms');
  return drupal_get_form('shusy_errand_shusy_errand_order_entity_form', $e, 'add');
}

function shusy_errand_shusy_errand_order_view_page(ShusyErrandOrder $e) {
  return entity_view($e->entityType(), [$e]);
}

function shusy_errand_shusy_errand_order_edit_page(ShusyErrandOrder $e) {
  module_load_include('inc', 'shusy_errand', 'shusy_errand.forms');
  return drupal_get_form('shusy_errand_shusy_errand_order_entity_form', $e, 'edit');
}

function shusy_errand_shusy_errand_order_delete_page(ShusyErrandOrder $e) {
  module_load_include('inc', 'shusy_errand', 'shusy_errand.forms');
  return drupal_get_form('shusy_errand_shusy_errand_order_delete_form', $e);
}

function shusy_errand_bind_user_edit_page($user) {
  $es = shusy_errand_load_multiple(FALSE, ['uid' => $user->uid]);
  if (!empty($es)) {
    $e = reset($es);
  }
  else {
    $e = new ShusyErrand();
    $e->uid = $user->uid;
  }
  module_load_include('inc', 'shusy_errand', 'shusy_errand.forms');
  return drupal_get_form('shusy_errand_shusy_errand_entity_form', $e, 'edit');
}

function shusy_errand_qr_page() {
  $uri = wx_mini_qr_create('shusy_errand', 'errandApply', 'errandApply', [
    'width' => 300,
    'line_color' => ['r' => 255, 'g' => 0, 'b' => 0],
  ]);
  return format_string("<img src='@src' width='300'>", ['@src' => file_create_url($uri)]);
}
